﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Web;
using Toolkit.Web.Helpers;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public interface IDataGridColumnInputModel
    {
        string Key
        {
            get;
            set;
        }

        int? Width
        {
            get;
            set;
        }

        int? Index
        {
            get;
            set;
        }

        int? RenderedIndex
        {
            get;
            set;
        }

        bool IsVisible
        {
            get;
            set;
        }

        IList<IDataGridColumnFilterInputModel> Filters
        {
            get;
            set;
        }

        void CleanModel();
    }

    [DataContract]
    public class DataGridColumnInputModel : IDataGridColumnInputModel, IPersistentDataGridColumnModel
    {
        public DataGridColumnInputModel()
        {
        }

        public DataGridColumnInputModel(IPersistentDataGridColumnModel settings)
            : this()
        {
            Key = settings.Key;
            Width = settings.Width;
            Index = settings.Index;
            IsVisible = settings.IsVisible;
            Filters = ((List<DataGridColumnFilterInputModel>)settings.Filters);
        }

        [DataMember]
        public string Key
        {
            get;
            set;
        }

        [DataMember]
        public int? Width
        {
            get;
            set;
        }

        [DataMember]
        public int? Index
        {
            get;
            set;
        }

        [DataMember]
        public int? RenderedIndex
        {
            get;
            set;
        }

        [DataMember]
        public bool IsVisible
        {
            get;
            set;
        }

        [DataMember]
        public List<DataGridColumnFilterInputModel> Filters
        {
            get;
            set;
        }

        IList<IDataGridColumnFilterInputModel> IDataGridColumnInputModel.Filters
        {
            get
            {
                if (Filters != null)
                {
                    return ((IEnumerable<IDataGridColumnFilterInputModel>)Filters).ToList();
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    Filters = ((List<DataGridColumnFilterInputModel>)value);
                }
                else
                {
                    Filters = null;
                };
            }
        }

        IList<IPersistentDataGridColumnFilterModel> IPersistentDataGridColumnModel.Filters
        {
            get
            {
                if (Filters != null)
                {
                    return ((IEnumerable<IPersistentDataGridColumnFilterModel>)Filters).ToList();
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    Filters = ((List<DataGridColumnFilterInputModel>)value);
                }
                else
                {
                    Filters = null;
                };
            }
        }

        public void CleanModel()
        {
            if (Filters != null)
            {
                foreach (DataGridColumnFilterInputModel filter in Filters)
                {
                    filter.CleanModel();
                }
            }
        }
    }
}
